package com.example.innovate.service.impl;

import com.example.innovate.domain.CreatedClassDO;
import com.example.innovate.domain.CreatedClassDOExample;
import com.example.innovate.mapper.CreatedClassDOMapper;
import com.example.innovate.service.CreatedClassService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class CreatedClassServiceImpl implements CreatedClassService {

    @Autowired
    CreatedClassDOMapper createdClassDOMapper;


    @Override
    public CreatedClassDO addCreatedClass(CreatedClassDO createdClassDO) {
        createdClassDOMapper.insertSelective(createdClassDO);
        return createdClassDO;
    }

    @Override
    public void deleteCreatedClass(int id) {
        createdClassDOMapper.deleteByPrimaryKey(id);
    }

    @Override
    public CreatedClassDO updateCreatedClass(CreatedClassDO createdClassDO) {
        createdClassDOMapper.updateByPrimaryKeySelective(createdClassDO);
        return createdClassDO;
    }

    @Override
    public List<CreatedClassDO> findCreatedClassByName(String baseName) {
        CreatedClassDOExample createdClassDOExample = new CreatedClassDOExample();
        createdClassDOExample.createCriteria()
                .andClassBaseNameEqualTo(baseName);
        List<CreatedClassDO> list = createdClassDOMapper.selectByExample(createdClassDOExample);
        return list;
    }

    @Override
    public CreatedClassDO findCreatedClassById(int id) {
        CreatedClassDOExample createdClassDOExample = new CreatedClassDOExample();
        createdClassDOExample.createCriteria()
                .andClassIdEqualTo(id);
        List<CreatedClassDO> list = createdClassDOMapper.selectByExample(createdClassDOExample);
        if(!list.isEmpty()){
            return list.get(0);
        }
        return null;
    }

    @Override
    public CreatedClassDO findByImage(String filename) {
        CreatedClassDOExample createdClassDOExample = new CreatedClassDOExample();
        createdClassDOExample.createCriteria()
                .andClassIntroduceImageEqualTo(filename);
        List<CreatedClassDO> list = createdClassDOMapper.selectByExample(createdClassDOExample);
        if(!list.isEmpty()){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<CreatedClassDO> findAll(int pageNumber, int pageSize) {
        CreatedClassDOExample createdClassDOExample = new CreatedClassDOExample();
        PageHelper.startPage(pageNumber,pageSize);
        List<CreatedClassDO> list = createdClassDOMapper.selectByExample(createdClassDOExample);
        return list;
    }

    @Override
    public List<CreatedClassDO> findAllByClassName(String className, int pageNumber, int pageSize) {
        CreatedClassDOExample createdClassDOExample = new CreatedClassDOExample();
        createdClassDOExample.createCriteria()
                .andClassNameEqualTo(className);
        PageHelper.startPage(pageNumber,pageSize);
        List<CreatedClassDO> list = createdClassDOMapper.selectByExample(createdClassDOExample);
        return list;
    }

    @Override
    public List<CreatedClassDO> findCreatedClassPage(Integer pageNumber, Integer pageSize,
                                                     String className,
                                                     String startTime, String endTime) {
        CreatedClassDOExample createdClassDOExample = new CreatedClassDOExample();
        createdClassDOExample.createCriteria()
                .andClassNameEqualTo(className)
                .andClassFoundTimeBetween(startTime,endTime);
        PageHelper.startPage(pageNumber,pageSize);
        List<CreatedClassDO> list = createdClassDOMapper.selectByExample(createdClassDOExample);
        return list;
    }
}
